/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.dao;

import core.APersistenceManager;
import core.IUserDAO;
import java.math.BigDecimal;
import javax.persistence.Query;
import persistence.*;
import validators.UserValidator;

/**
 *
 * @author Ivan
 */
public class UserDAO extends APersistenceManager<SysUser> implements IUserDAO {

    public UserDAO() {
        super(SysUser.class, new UserValidator());
    }

    @Override
    public SysUser authorize(String login, String password) {
      Query q =  getEntityManager().createNamedQuery("SysUser.authorize");
       q.setParameter("login", login);
       q.setParameter("password", password);
       return (SysUser) q.getSingleResult();
    }

    
    @Override
    public SysUser getOwnerByGame(int idGame) {
        Game game = getEntityManager().find(Game.class, BigDecimal.valueOf(idGame));
        return game.getOwner();
    }

    @Override
    public SysUser getOwnerByLeague(int idLeague) {
        League league = getEntityManager().find(League.class, BigDecimal.valueOf(idLeague));
        return league.getOwner();
    }

    @Override
    public SysUser getOwnerByPlayer(int idPlayer) {
        League league = getEntityManager().find(League.class, BigDecimal.valueOf(idPlayer));
        return league.getOwner();
    }

    @Override
    public SysUser getOwnerByTeam(int idTeam) {
        Team team = getEntityManager().find(Team.class, BigDecimal.valueOf(idTeam));
        return team.getOwner();
    }

    @Override
    public SysUser getOwnerByTournament(int idTournament) {
        Tournament tournament = getEntityManager().find(Tournament.class, BigDecimal.valueOf(idTournament));
        return tournament.getOwner();
    }
}
